package pl.edu.agh.uddiProxy;

import java.util.HashSet;
import java.util.Set;

import javax.xml.namespace.QName;

import pl.edu.agh.uddiProxy.dao.DAOHelper;
import pl.edu.agh.uddiProxy.query.FindTModelQuery;
import pl.edu.agh.uddiProxy.query.SaveTModelQuery;
import pl.edu.agh.uddiProxy.types.Authority;
import pl.edu.agh.uddiProxy.types.TModel;
import pl.edu.agh.uddiProxy.types.User;
import dk.itst.uddi.client.Util;
import dk.itst.uddi.client.query.SaveBindingQuery;
import dk.itst.uddi.client.query.SaveServiceQuery;
import dk.itst.uddi.client.types.core.BusinessEntity;
import dk.itst.uddi.client.types.core.BusinessService;
import dk.itst.uddi.client.types.core.OverviewDoc;
import dk.itst.uddi.client.types.core.OverviewURL;
import dk.itst.uddi.client.types.core.TModelInfo;

public class App {
	public static void main(String[] args) {
//		create("mojaFirma", "usluga pocztowa");
//		create("bank", "usluga bankowa");
//		create("firma", "usluga obliczeniowa");
		
//		Set<Authority> set = new HashSet<Authority>();
//		set.add(Authority.USER);
//		User user = new User("wojtek", "firstName", "lastName", "wojtek", "email", set);
//		DAOHelper.getUserDAO().create(user);
//		set.add(Authority.ADMIN);
//		user = new User("admin", "firstName", "lastName", "admin", "email", set);
//		DAOHelper.getUserDAO().create(user);
		
//		TModel model = DAOHelper.getUDDIClient().getInquiryClient().getTModel("uddi:eos.dk:fd7794f1-9ee2-4ead-a76a-fb844f2883fe");
//		System.out.println(DAOHelper.getUDDIClient().getInquiryClient().getOperationalInfo(model.getModel().getTModelKey()));
		

//		FindTModelQuery query = DAOHelper.getUDDIClient().getInquiryClient().findTModel().setName("uddi:uddi.org:ubr:categorization:iso3166");
//		TModelInfo info = query.execute()[0];
//		String category = info.getTModelKey();
		
		TModelInfo[] modelInfo = DAOHelper.getUDDIClient().getInquiryClient().findTModel().addCategory(null, "PL", "uddi:uddi.org:ubr:categorization:iso3166").execute();
		for (TModelInfo i : modelInfo) {
			System.out.println(i.getTModelKey());
		}
	}
	
	private static void create(String businessName, String serviceName) {
		UDDIClient client = DAOHelper.getUDDIClient();

		BusinessEntity businessEntity = client.getPublication().saveBusiness().addName(businessName, "pl").execute();
		
		SaveServiceQuery saveServiceQuery = client.getPublication().saveService().addName(serviceName);
		saveServiceQuery.setBusinessKey(businessEntity.getBusinessKey());
		BusinessService businessService = saveServiceQuery.execute();
		
		SaveTModelQuery saveTModelQuery = client.getPublication().saveTModel().addDescription(serviceName + " HTTP", "pl");
		saveTModelQuery.setName("implementacja " + serviceName);
		OverviewDoc overviewDoc = saveTModelQuery.addOverviewDoc();
		OverviewURL overviewURL = overviewDoc.addNewOverviewURL();
		overviewURL.setUseType("wsdlInterface");
		overviewURL.setStringValue("http://" + serviceName.replaceAll("\\p{javaWhitespace}", "") + ".wsdl");
		TModel model = saveTModelQuery.execute();
		
		SaveBindingQuery saveBindingQuery = client.getPublication().saveBinding().addDescription(serviceName, "pl");
		saveBindingQuery.setAccessPoint("http", "http://" + businessName.replaceAll("\\p{javaWhitespace}", "") + "/" + serviceName.replaceAll(" ", "") + ":12345");
		saveBindingQuery.setServiceKey(businessService.getServiceKey());
		saveBindingQuery.addTModelInstance(model.getModel().getTModelKey());
		saveBindingQuery.execute();
	}
}
